﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BLL;
using Entities;
using DAL;
using System.Data.SqlClient;
using System.Configuration;
using BLToolkit.Data;


namespace CuaHangBangDia.DoanhThu
{
    public partial class CapNhatBanHangForm : Form
    {
        int item = 12;
        int page = 1;
        int tong;
        bool _selectAll = false;
        public CapNhatBanHangForm()
        {
            InitializeComponent();
        }

        private void LoadBanHang(List<BanHang> ds)
        {
            dtLoad.DataSource = ds;
            tong = TongTrang(BanHang.Objects.GetAll().ToList().Count, item);
            txtTrang.Text = Convert.ToString(page);
            lbTongTrang.Text = "/ " + tong;

        }

        public List<BanHang> getPage(int page)
        {
            dtLoad.Columns[1].DefaultCellStyle.Format = "dd/MM/yyyy";
            var query = from c in BanHang.Objects.GetAll().ToList()
                           .Skip(item * page - item).Take(item)
                        select c;
            return query.ToList();

        }

        public static int TongTrang(int soluongdl, int num)
        {
            int kq = (int)(soluongdl / num);
            if (kq * num == soluongdl)
                return kq;
            return kq + 1;
        }

        private void btnNext_Click(object sender, EventArgs e)
        {
            if (page < tong)
                page++;
            if (page <= tong)
            {
                txtTrang.Text = Convert.ToString(page);
                LoadBanHang(getPage(page));
            }
        }

        private void btnBack_Click(object sender, EventArgs e)
        {

            if (page >= 2)
            {
                page--;
                txtTrang.Text = Convert.ToString(page);
                LoadBanHang(getPage(page));
            }
        }

        private BanHang Getdata(int i)
        {
            BanHang bh = new BanHang();
            bh.NgayBan = Convert.ToDateTime(dtLoad.Rows[i].Cells[3].Value.ToString());
            bh.TienBan = Convert.ToDouble(dtLoad.Rows[i].Cells[4].Value.ToString());
            bh.TienChoThue = Convert.ToDouble(dtLoad.Rows[i].Cells[5].Value.ToString());
            double tien = Convert.ToDouble(dtLoad.Rows[i].Cells[4].Value);
            double tienthue = Convert.ToDouble(dtLoad.Rows[i].Cells[5].Value);
            bh.KhacChi = Convert.ToDouble(dtLoad.Rows[i].Cells[6].Value.ToString());
            bh.KhacThu = Convert.ToDouble(dtLoad.Rows[i].Cells[7].Value.ToString());
            bh.Tong = (tien + tienthue + bh.KhacThu) - bh.KhacChi;
            return bh;
        }

        private void dtLoad_ColumnHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            int n = dtLoad.Rows.Count;
            if (e.ColumnIndex == 0 && n > 0)
            {
                _selectAll = !_selectAll;
                for (int i = 0; i < n; i++)
                {
                    dtLoad.Rows[i].Selected = _selectAll;
                    dtLoad.Rows[i].Cells[0].Value = _selectAll;
                }


            }
        }

        private void frmCapNhatBanHang_Load(object sender, EventArgs e)
        {
            LoadBanHang(getPage(page));
            
        }

        private void dtLoad_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            string ten = dtLoad.Columns[e.ColumnIndex].Name;
            BanHang x = new BanHang();            

            if (ten == "btnSua")
            {
                for (int i = 0; i < dtLoad.Rows.Count; i++)
                {
                    if (dtLoad.Rows[i].Cells[0].Value != null)
                    {
                        x = Getdata(i);
                        if (BanHang.Objects.Update(x) == true)
                        {

                            MessageBox.Show("Sửa Thành Công","Thông Báo");
                            btnLoad.PerformClick();
                        }
                        
                    }

                }
            }
            if (ten == "btnXoa")
            {
                for (int i = 0; i < dtLoad.Rows.Count; i++)
                {
                    if (dtLoad.Rows[i].Cells[0].Value != null)
                    {
                        BanHang t = Getdata(i);
                        if (BanHang.Objects.Delete(t))
                        {
                            MessageBox.Show("Xóa Thành Công","Thông Báo");
                            btnLoad.PerformClick();
                        }
                    }
                }
            }
            if (ten == "cAll")
            {
                if (dtLoad.Rows.Count > 0)
                {
                    for (int i = 0; i < dtLoad.Rows.Count ; i++)
                    {
                        if (dtLoad.Rows[i].Cells[0].Value != null)
                        {
                            
                            dtLoad.Rows[i].Cells[1].ReadOnly = false;
                        }
                    }
                }
            }
        }

        private void dtLoad_CellValueChanged(object sender, DataGridViewCellEventArgs e)
        {
            for (int i = 0; i < dtLoad.Rows.Count; i++)
            {

                try
                {
                    //    }
                    dtLoad.Rows[i].Cells[8].Value = Convert.ToDouble(Convert.ToDouble(dtLoad.Rows[i].Cells[4].Value) + Convert.ToDouble(dtLoad.Rows[i].Cells[5].Value) + Convert.ToDouble(dtLoad.Rows[i].Cells[7].Value) - Convert.ToDouble(dtLoad.Rows[i].Cells[6].Value));
                }
                catch (Exception)
                {
                    if (dtLoad.Rows[i].Cells[6].Value.Equals(""))
                    {
                        dtLoad.Rows[i].Cells[6].Value = 0;
                    }
                    else if (dtLoad.Rows[i].Cells[7].Value.Equals(""))
                    {
                        dtLoad.Rows[i].Cells[7].Value = 0;
                    }
                }
            }
        }

        private void btnLoad_Click(object sender, EventArgs e)
        {
           
            LoadBanHang(getPage(page));
        }

        private void btnThoat_Click(object sender, EventArgs e)
        {
            this.Close();

        }
        public void TimTheoNgay(DateTime dt)
        {
            List<BanHang> kq = BanHang.Objects.Find(b => b.NgayBan.Equals(dt.ToString("MM/dd/yyyy"))).ToList();
            if (kq.Count > 0)
            {
                LoadBanHang(kq);
            }
            else
            {
                dtLoad.DataSource = null;
                MessageBox.Show("Không Tìm Thấy", "Thông Báo");

            }
        }
        private void TimTheoThang(DateTime dt)
        {
            List<BanHang> kq = BanHang.Objects.Find(b => b.NgayBan.Year == (dt.Year) && b.NgayBan.Month == (dt.Month)).ToList();
            if (kq.Count > 0)
            {
                LoadBanHang(kq);
            }
            else
            {
                dtLoad.DataSource = null;
                MessageBox.Show("Không Tìm Thấy", "Thông Báo");

            }
        }
        private void TimTheoNam(DateTime dt)
        {
            List<BanHang> kq = BanHang.Objects.Find(b => b.NgayBan.Year == dt.Year).ToList();
            if (kq.Count > 0)
            {
                LoadBanHang(kq);
            }
            else
            {
                dtLoad.DataSource = null;
                MessageBox.Show("Không Tìm Thấy", "Thông Báo");

            }
        }

        private void btnTK_Click(object sender, EventArgs e)
        {
            if (rbNgay.Checked)
            {
                TimTheoNgay(dtpNgayNhap.Value);
            }
            else if (rbThang.Checked)
            {
                TimTheoThang(dtpNgayNhap.Value);
            }
            else if (rbNam.Checked)
            {
                TimTheoNam(dtpNgayNhap.Value);
            }
            else
            {
                MessageBox.Show("Chọn Kiểu Tìm Kiếm", "Thông Báo");
            }
        }

        private void btnHuy_Click(object sender, EventArgs e)
        {
            if (dtLoad.Rows.Count > 0)
            {
                for (int i = 0; i < dtLoad.Rows.Count ; i++)
                {
                    
                        
                        dtLoad.Rows[i].Cells[0].Value = null;
                    
                }
            }

        }
        private bool KiemTraNull(object a, object b)
        {

            if ((a == null && b == null) || a == null || b == null)
            {
                MessageBox.Show("Nhập Giá Trị cho Tiền Bán và Tiền Cho Thuê");
                return true;
            }
            return false;
        }
        private bool KiemTraNhapSo(object a)
        {

            if (a is String || a is Char)
            {
                MessageBox.Show("Nhập Giá Trị Là Số");
                return true;
            }

            return false;
        }

        private void dtLoad_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e)
        {
            
        }

        private void txtTrang_KeyDown(object sender, KeyEventArgs e)
        {
            int t;
            if (e.KeyData == Keys.Enter)
            {
                try
                {
                    t = Convert.ToInt32(txtTrang.Text);
                    if (t <= tong && t > 0)
                    {
                        LoadBanHang(getPage(t));
                        txtTrang.Text = Convert.ToString(t);
                        page = t;
                    }
                }
                catch (Exception)
                {
                    MessageBox.Show("Nhâp Trang Muốn Đến");
                }

            }
        }

        

       
    }
}
